package org.ww.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.Query;
import org.ww.ware.dao.PurchaseDetailDao;
import org.ww.ware.entity.PurchaseDetailEntity;
import org.ww.ware.service.PurchaseDetailService;

import java.util.List;
import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByParams(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = Wrappers.lambdaQuery();

        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            wrapper.and(w -> w.eq(PurchaseDetailEntity::getPurchaseId,key).or().eq(PurchaseDetailEntity::getSkuId,key));
        }

        String status = (String) params.get("status");
        if(StringUtils.isNotBlank(status)){
            wrapper.eq(PurchaseDetailEntity::getStatus,status);
        }

        String wareId = (String) params.get("wareId");
        if(StringUtils.isNotBlank(wareId)){
            wrapper.eq(PurchaseDetailEntity::getWareId,wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List<PurchaseDetailEntity> getByPurchaseIds(List<Long> purchaseIds) {
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(PurchaseDetailEntity::getPurchaseId,purchaseIds);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public void updateStatusByPurhcaseIds(List<Long> purchaseIds, Integer status) {
        LambdaUpdateWrapper<PurchaseDetailEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(PurchaseDetailEntity::getPurchaseId,purchaseIds);
        updateWrapper.set(PurchaseDetailEntity::getStatus,status);
        baseMapper.update(null,updateWrapper);
    }

    @Override
    public List<PurchaseDetailEntity> getByIds(List<Long> purchaseItemIds) {
        return baseMapper.selectBatchIds(purchaseItemIds);
    }

}